package com.solo.eggsolo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.solo.eggsolo.common.bean.JsonResult;
import com.solo.eggsolo.common.bean.Pager;
import com.solo.eggsolo.entity.Song;
import com.solo.eggsolo.service.SongService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @RestController控制器默认会返回Body
 * @author yu
 *
 */
@RestController
@RequestMapping("/song")
@Api(tags = "歌曲管理")
public class SongController {

	@Autowired
	private SongService songService;

	@ApiOperation("获取歌曲所有信息")
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	public Pager<Song> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer size) {
		Pager<Song> pager = new Pager<>(page, size);

		List<Song> list = songService.list(pager);
		int total = songService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);

		return pager;
	}

	@ApiOperation("根据歌曲id查询歌曲信息")
	@RequestMapping(value = "/findById", method = { RequestMethod.GET })
	public JsonResult findById( Integer id) {

		JsonResult result = new JsonResult(500, Boolean.FALSE, "查询失败");
		Song song = songService.findById(id);

		if (song != null) {
			result = new JsonResult(200, Boolean.TRUE, "查询成功", song);
		}
		return result;
	}
	

	@ApiOperation("根据歌曲名称模糊查询歌曲信息")
	@RequestMapping(value = "/findByName", method = { RequestMethod.GET })
	public Pager<Song> findByName(Song song, String songName) {

		Pager<Song> pager = new Pager<>(1, 100);
		List<Song> list = songService.findByName(songName);
		int total = songService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);
		
		return pager;
	}
	@ApiOperation("查询推荐歌曲")
	@RequestMapping(value = "/findRecommendSong", method = { RequestMethod.GET })
	public Pager<Song> findRecommendSong() {

		Pager<Song> pager = new Pager<>(1, 100);
		List<Song> list = songService.findRecommendSong();
		int total = songService.count(pager);
		pager.setData(list);
		pager.setTotal(total);
		pager.setCode(200);
		
		return pager;
	}

	@ApiOperation("增加歌曲信息")
	@RequestMapping(value = "/add", method = { RequestMethod.POST })
	public JsonResult add(Song recommend) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "新增失败");

		boolean ret = songService.add(recommend);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "新增成功");
		}
		return result;
	}
	
	@ApiOperation("修改推荐歌曲")
	@RequestMapping(value = "/editRecommendSong", method = { RequestMethod.POST })
	public JsonResult editRecommendSong(Integer isRecommend,Integer id) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "修改失败");

		boolean ret = songService.editRecommendSong(isRecommend,id);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "修改成功");
		}
		return result;
	}

	@ApiOperation("根据歌曲id编辑歌曲信息")
	@RequestMapping(value = "/edit", method = { RequestMethod.POST })
	public JsonResult edit(Song song) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "修改失败");

		// 先上传后更新（保存）
		boolean ret = songService.edit(song);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "修改成功");
		}
		return result;
	}

	@ApiOperation("根据歌曲id删除歌曲信息")
	@RequestMapping(value = "delete", method = { RequestMethod.POST })
	public JsonResult delete(Song song) {
		JsonResult result = new JsonResult(500, Boolean.FALSE, "删除失败");

		List<Song> list = new ArrayList<Song>();
		list.add(song);

		boolean ret = songService.deleteByIds(list);
		if (ret) {
			result = new JsonResult(200, Boolean.TRUE, "删除成功");
		}
		return result;
	}

}
